﻿using System;
using System.Linq;
#if WPF
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using Brush = System.Windows.Media.Brush;
using Point = System.Windows.Point;
using Rectangle = System.Windows.Shapes.Rectangle;
using System.Windows.Shapes;
#else
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Shapes;
using Windows.UI.Xaml.Data;
using Windows.Foundation;
using Windows.UI.Xaml.Media;
using Windows.UI;
#endif



namespace Eternal.DataVisualization
{
    public class ColumnSeries : DepthType
    {
        internal override void UpdateRange()
        {
            base.UpdateRange();
            if (YAxis != null)
                YAxis.ActualStart = 0;
        }

        protected internal override void OnDrawSegment()
        {
            base.OnDrawSegment();
            SegmentDistance segmentDistance;
            if (Area != null)
                segmentDistance = Area.GetSegmentDepth(this);
            else
                segmentDistance = new SegmentDistance { Delta = 0.0, Start = -0.4, End = 0.4 };
            var segStart = segmentDistance.Start;
            var segEnd = segmentDistance.End;
#if WPF_UN
            if (RenderPerformance == PerformanceType.Normal)
#endif
            {
                SegmentInfo info;
                SegmentPanel.Children.Clear();
                for (var i = (int)Math.Floor(XAxis.Start); i <= (int)Math.Ceiling(XAxis.End); i++)
                {
                    if (ActualYData.Count <= i || i < 0) continue;
                    var yValue = ActualYData[i];
                    var rectangle = new Rectangle();
                    SegmentPanel.Children.Add(rectangle);
                    info = new SegmentInfo();
                    SegmentInfos.Add(info);
                    ApplyBrush(rectangle, i);
                    var value1 = new Point(i + segStart, Convert.ToDouble(yValue));
                    var value2 = new Point(i + segEnd, 0);
                    var center = new Point(i, Convert.ToDouble(yValue));

                    var rect = new Rect(new Point(ValueToCoordianteXAxis(value1.X), ValueToCoordianteYAxis(value1.Y)), new Point(ValueToCoordianteXAxis(value2.X), ValueToCoordianteYAxis(value2.Y)));
                    info.Point = new Point(ValueToCoordianteXAxis(value1.X + segmentDistance.Delta / 2), rect.Top);
                    info.Value = new Point(i, yValue);
                    info.Series = this;
                    rectangle.Tag = info;
                    rectangle.Width = rect.Width;
                    rectangle.Height = rect.Height;
                    rectangle.SetValue(Canvas.LeftProperty, rect.Left);
                    rectangle.SetValue(Canvas.TopProperty, rect.Top);

                    if (Label != null)
                    {
                        Label.Positions.Add(info);
                    }
                }
                ChartAnimation.AnimateRectangle(SegmentPanel.Children.OfType<Rectangle>().ToList());
            }
#if WPF_UN
            else
            {
                var actualBrush = GetBrush();
                var color = (actualBrush as SolidColorBrush).Color;
                writeableBitmap.Lock();
                writeableBitmap.Clear();
                for (var i = 0; i < ActualXData.Count; i++)
                {
                    writeableBitmap.FillRectangle((int)ValueToCoordianteXAxis(i + segStart), (int)ValueToCoordianteYAxis(ActualYData[i]), (int)ValueToCoordianteXAxis(i + segEnd), (int)ValueToCoordianteYAxis(0), color);
                }
                writeableBitmap.Unlock();
            }
#endif
        }

        protected override void MinMaxChange()
        {
            //MinimumX -= 0.4;
            //MaximumX -= 0.6;
            //DeltaX = MaximumX - MinimumX;
            //MinimumY = 0;
            //DeltaY = MaximumY - MinimumY;
            base.MinMaxChange();
        }

        private Brush highlightBrush;

        public Brush HighlightBrush
        {
            get { return highlightBrush; }
            set { highlightBrush = value; }
        }

        Rectangle previousElement;
#if WPF
        protected override void OnMouseMove(System.Windows.Input.MouseEventArgs e)
#else
        protected override void OnPointerMoved(Windows.UI.Xaml.Input.PointerRoutedEventArgs e)
#endif
        {
            if (previousElement != null)
            {
                var brush = (previousElement.Fill as SolidColorBrush).Color;
                previousElement.Fill = new SolidColorBrush(Color.FromArgb(255, (byte)(brush.R / 0.6), (byte)(brush.G / 0.6), (byte)(brush.B / 0.6)));
                previousElement.StrokeThickness = 0;
                previousElement = null;
            }
            var rect = e.OriginalSource as Rectangle;

            if (rect != null)
            {
                var brush = (rect.Fill as SolidColorBrush).Color;
                rect.Fill = new SolidColorBrush(Color.FromArgb(255, (byte)(brush.R * 0.6), (byte)(brush.G * 0.6), (byte)(brush.B * 0.6)));
                previousElement = rect;
            }
#if WPF
            base.OnMouseMove(e);
#else
            base.OnPointerMoved(e);
#endif
        }
    }
}
